use libc::{__errno_location, close, execlp, exit, fprintf, open, printf, setns, sprintf, strerror, O_RDONLY};
use std::ffi::{c_char, c_void, CStr};
use std::os::unix::raw::pid_t;
use std::ptr::null;

pub fn lib_namespace_exec(pid: *const c_char, execute_able: *const c_char) {
    unsafe {
        let namespace_file = [c"pid", c"ipc", c"net", c"uts", c"user", c"mnt"];
        for file in namespace_file {
            let mut namespace_file_path = [0 as c_char; 1024];
            sprintf(
                namespace_file_path.as_mut_ptr(),
                c"/proc/%s/ns/%s".as_ptr(),
                pid,
                file.as_ptr(),
            );
            let fd = open(namespace_file_path.as_ptr(), O_RDONLY);
            if setns(fd,0)==-1 {
                printf(c"%s\n".as_ptr(),strerror(*__errno_location()));
                exit(3);
            }else {
                printf(c"into name space %s sucess\n".as_ptr(),file.as_ptr());
            }
            close(fd);
        }
        let i = execlp(execute_able, execute_able, null::<c_void>());
        printf(c"%s\n".as_ptr(),execute_able);
        printf(c"%s\n".as_ptr(),strerror(*__errno_location()));
        exit(3);


    }
}
